<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 角色管理部分 -->
          <el-tab-pane label="角色管理">
            <!-- 新增按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="newAddEditList('新增员工')"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border :data="list">
              <el-table-column
                label="序号"
                width="120"
                align="center"
              >
                <template #default="scope">
                  {{ (page.page -1 ) * page.pagesize + scope.$index +1 }}
                </template>
              </el-table-column>
              <el-table-column
                label="角色名称"
                width="240"
                prop="name"
                align="center"
              />
              <el-table-column
                label="描述"
                prop="description"
                align="center"
              />
              <el-table-column
                label="操作"
                align="center"
              >
                <template #default="{row}">
                  <!-- @click="assignPerm(row.id)" -->
                  <el-button type="success" size="small" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button type="primary" size="small" @click="newAddEditList('编辑员工信息',row)">编辑</el-button>
                  <el-button type="danger" size="small" @click="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">

              <el-pagination
                layout="prev, pager, next"
                :total="page.total"
                :current-page="page.page"
                :page-size="page.pagesize"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 公司信息消息 -->

            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="活动区域">
                <el-input v-model="formData.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled placeholder="北京市昌平区建材城西路金燕龙办公楼一层" style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled placeholder="bd@itcastcn" style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" disabled placeholder="传智播客官网-好口碑IT培训机构,一样的教育,不一样的品质" type="textarea" :rows="3" style="width: 400px" />
              </el-form-item>
            </el-form>

          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 编辑对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="addEdit"
        width="50%"
        @close="closeDialog"
      >
        <el-form ref="formRef" label-width="120px" :model="roleForm" :rules="rules">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="addEdit = false">取 消</el-button>
            <el-button size="small" type="primary" @click="editAddList">确 定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <!-- 分配权限对话框 -->
      <el-dialog
        title="分配权限"
        :visible.sync="allocation"
        width="50%"
      >
        <el-tree
          :data="data"
          show-checkbox
          :props="{label: 'name'}"
          default-expand-all
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="allocation = false">取 消</el-button>
            <el-button size="small" type="primary" @click="allocation= false">确 定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

    </div>
    <!--  -->
    <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
      <!-- 权限是一颗树 -->
      <!-- 将数据绑定到组件上 -->
      <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
      <!-- id作为唯一标识 -->
      <el-tree
        ref="permTree"
        :data="permData"
        :props="defaultProps"
        :show-checkbox="true"
        :check-strictly="true"
        :default-expand-all="true"
        :default-checked-keys="selectCheck"
        node-key="id"
      />
      <!-- 确定 取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
          <el-button size="small" @click="btnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  delRoleList,
  updateRole,
  addRoleList,
  getPermissions
  , assignPerm,
  getRoleDetail } from '@/api/setting'
import { tranListToTreeData } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      // 所有角色列表
      list: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 0
      },
      // 公司信息
      formData: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      // 对话框开关
      addEdit: false,
      // 对话框内容
      // 专门接收新增或者编辑的编辑的表单数据
      roleForm: {},
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      // 弹框标题
      title: '',
      // 分配权限弹框 开关
      allocation: false,
      // 所有的权限
      data: [],
      showPermDialog: false, // 控制分配权限弹层的显示后者隐藏
      defaultProps: {
        label: 'name'
      },
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null // 用来记录分配角色的id
    }
  },
  computed: {
    ...mapGetters['companyId']
  },
  created() {
    this.getRoleList()// 获取所有角色列表
    this.getCompanyInfo()// 获取公司信息
  },
  methods: {
    // 获取所有角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage// 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    // 获取公司信息
    async getCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      this.formData = res
    },

    // 点击编辑按钮// 点击添加按钮
    newAddEditList(title, row) {
      this.addEdit = true
      if (title === '编辑员工信息') {
        this.title = title
        this.roleForm = { ...row }
      } else {
        this.title = title
      }
    },
    // 确定按钮
    async editAddList() {
      try {
        await this.$refs.formRef.validate()
        if (this.title === '编辑员工信息') {
          console.log(this.roleForm)
          await updateRole(this.roleForm)
          this.$message.success('编辑成功')
        } else {
          await addRoleList(this.roleForm)
          this.$message.success('添加员工成功')
        }
        this.getRoleList()
        this.addEdit = false
      } catch (err) {
        console.log(err)
      }
    },
    // 关闭对话框
    closeDialog() {
      this.roleForm = {}
      this.$refs.formRef.resetFields()
    },
    // 分配权限弹框
    // 点击分配权限
    // 获取权限点数据 在点击的时候调用 获取权限点数据
    async assignPerm(id) {
      this.permData = tranListToTreeData(await getPermissions(), '0') // 转化list到树形数据
      this.roleId = id
      // 应该去获取 这个id的 权限点
      // 有id 就可以 id应该先记录下来
      const { permIds } = await getRoleDetail(id) // permIds是当前角色所拥有的权限点数据
      this.selectCheck = permIds // 将当前角色所拥有的权限id赋值
      this.showPermDialog = true
    },
    async  btnPermOK() {
      // 调用el-tree的方法
      // console.log(this.$refs.permTree.getCheckedKeys())
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    btnPermCancel() {
      this.selectCheck = [] // 重置数据
      this.showPermDialog = false
    },
    // async allot(row) {
    //   const roles = await getRoleDetails(row.id)
    //   const res = await getPermissions()
    //   this.data = tranListToTreeData(res, '0')
    //   console.log(this.data)
    //   console.log(roles)
    //   this.allocation = true
    // },
    // 删除员工
    async  del(id) {
      this.$confirm('此操作将永久删除该员工, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoleList(id)
        this.$message.success('删除角色成功')
        this.getRoleList()// 获取所有角色列表
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
